//
//  LJEmailTextField.m
//  LJEmailTextField
//
//  Created by liujinlong on 15/3/12.
//  Copyright (c) 2015年 com.ljl. All rights reserved.
//

#import "LJEmailListView.h"
#import "NSString+MailString.h"

@interface LJEmailListView ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

/** 显示邮箱俩表的tableView */
@property (nonatomic, strong) UITableView * mailListTableView;

/** 输入框 */
@property (nonatomic, strong) UITextField * inputTextField;

/** 要显示的邮箱列表数据 */
@property (nonatomic, strong) NSArray * mailList;

@end

@implementation LJEmailListView
{
    CGFloat _maxH; //最大高度，根据textField的位置计算得到
}

- (instancetype)initWithTextFiled:(UITextField *)textField
{
    CGRect textFieldFrame = textField.frame;
    
    CGFloat viewX = textFieldFrame.origin.x;
    CGFloat viewY = CGRectGetMaxY(textFieldFrame);
    CGFloat viewW = CGRectGetWidth(textFieldFrame);
    _maxH = [UIScreen mainScreen].bounds.size.height - viewY - kBottomInterval;
    CGFloat viewH = _maxH > kEmailCellHeigh * kMailList.count ? kEmailCellHeigh * kMailList.count : _maxH;
    CGRect viewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
    if (self = [super initWithFrame:viewFrame])
    {
        self.inputTextField = textField;
        self.inputTextField.delegate = self;
        
        //初始化TableView
        self.mailListTableView                   = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, viewW, viewH)];
        self.mailListTableView.delegate          = self;
        self.mailListTableView.dataSource        = self;
        self.mailListTableView.layer.borderColor = [[UIColor grayColor] CGColor];
        self.mailListTableView.layer.borderWidth = 0.5;
        self.mailListTableView.separatorStyle    = UITableViewCellSeparatorStyleSingleLine;
        self.mailListTableView.separatorInset    = UIEdgeInsetsMake(0, 0, 0, 0);
        self.mailListTableView.rowHeight         = kEmailCellHeigh;
        [self addSubview:self.mailListTableView];
        
        self.mailList = [[NSArray alloc] init];
        self.hidden = YES;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:self.inputTextField];
    }
    return self;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.inputTextField.text = self.mailList[indexPath.row];
    [self.inputTextField resignFirstResponder];
    self.hidden = YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.inputTextField resignFirstResponder];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mailList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mailCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mailCell"];
    }
    cell.textLabel.text = self.mailList[indexPath.row];
    return cell;
}

#pragma mark - testfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.hidden = YES;
    
    if (textField.returnKeyType == UIReturnKeyDone) {
        //点击键盘上的Done按钮，隐藏仅安排，自动用第一个匹配的邮箱填充
        [textField resignFirstResponder];
        if (self.mailList.count > 0) {
            textField.text = self.mailList.firstObject;
        }
    }
    return YES;
}

#pragma mark - inputTextFieldTextDidChange Notification handle
- (void)inputTextFieldTextDidChange:(NSNotification *)notify
{
    [self handleMatchText];
}

#pragma mark - match text
- (void)handleMatchText
{
    //是否显示maillist
    if ([self.inputTextField.text showMailList]) {
        NSArray * mailList = [self.inputTextField.text matchMailListWithMailList:kMailList];
        self.mailList = mailList;
        if (!mailList.count) {
            self.hidden = YES;
        }
        else
        {
            [self adjustTableHeight];
            [self.mailListTableView reloadData];
            self.hidden = NO;
        }
        return;
    }
    else
    {
        self.hidden = YES;
    }
}

- (void)adjustTableHeight
{
    //根据当前匹配的条数重新调整tableview的高度
    CGRect f = self.frame;
    f.size.height = self.mailList.count * kEmailCellHeigh > _maxH ? _maxH : self.mailList.count * kEmailCellHeigh;
    self.frame = f;
    self.mailListTableView.frame = self.bounds;
}


@end
